#include "stdio.h"
#include "stdlib.h"

struct worker {
    int number;
    int date[3];
    int start[3];
    int end[3];
    struct worker *next;
};

struct worker * creat_table();
void input(struct worker *b);
void output(struct worker *c);
void sort_by_number(struct worker *d);
void sort_by_start(struct worker *e);
void sort_by_end(struct worker *f);
void best(struct worker *g);
void save(struct worker *h);
void reads(struct worker *i);

int main(){
    int a;
    struct worker *head;
    printf("1.输入员工考勤信息。\n2.按照员工号有序输出考勤信息。\n3.按照进公司时间有序输出考勤信息。\n4.按照离开公司时间有序输出考勤信息。\n5.输出优秀员工考勤信息，即当天工作时间最长的员工。\n6.员工考勤信息存入文件，文件名以日期命名。\n7.读取输入的日期，显示那天员工的考勤信息。\n");
    head=creat_table();
    while (scanf("%d",&a)!=EOF) {
        switch (a) {
            case 1:input(head);break;
            case 2:sort_by_number(head);break;
            case 3:sort_by_start(head);break;
            case 4:sort_by_end(head);break;
            case 5:best(head);break;
            case 6:save(head);break;
            case 7:reads(head);break;
            default:printf("no such operation\n");
        }
    }
    return 0;
}

struct worker * creat_table(){
    struct worker *head = NULL,*pf = NULL,*pb;
    for (int i=0; i<3; i++) {
        pb=(struct worker *)malloc(sizeof(struct worker));
        if (i==0) {
            pf=head=pb;
        }
        else{
            pf->next=pb;
        }
        pb->next=NULL;
        pf=pb;
    }
    return head;
}

void input(struct worker *b){
    while (b!=NULL) {
        scanf("%d %d %d %d %d %d %d %d %d %d\n",&(b->number),&(b->date[0]),&(b->date[1]),&(b->date[2]),&(b->start[0]),&(b->start[1]),&(b->start[2]),&(b->end[0]),&(b->end[1]),&(b->end[2]));
        b=b->next;
    }
}

void output(struct worker *c){
    while (c!=NULL) {
        printf("%d %d-%d-%d %d:%d:%d %d:%d:%d\n",c->number,c->date[0],c->date[1],c->date[2],c->start[0],c->start[1],c->start[2],c->end[0],c->end[1],c->end[2]);
        c=c->next;
    }
}

void sort_by_number(struct worker *d){
    struct worker *k;
    k=d;
    for (int i=0; i<2; i++) {
        d=k;
        for (int j=0; j<2-i; j++) {
            if ((d->number)>((d->next)->number)) {
                struct worker temp;
                temp=*d;
                *d=*(d->next);
                *(d->next)=temp;
            }
            d=d->next;
        }
    }
    output(k);
}

void sort_by_start(struct worker *e){
    double min[3];
    int t=0;
    struct worker *h,*n,*a;
    a=n=h=e;
    while (h!=NULL) {
        min[t]=(h->start[0])*60+h->start[1]+(h->start[2])/60;
        t++;
        h=h->next;
    }
    for (int i=0; i<2; i++) {
        a=e;
        for (int j=0; j<2-i; j++) {
            if(min[j]>min[j+1]){
                double temp;
                temp=min[j];
                min[j]=min[j+1];
                min[j+1]=temp;
                struct worker te;
                te=*a;
                *a=*(a->next);
                *(a->next)=*a;
            }
            a=a->next;
        }
    }
    output(n);
}

void sort_by_end(struct worker *f){
    double min[3];
    int t=0;
    struct worker *h,*n,*a;
    a=n=h=f;
    while (h!=NULL) {
        min[t]=(h->end[0])*60+h->end[1]+(h->end[2])/60;
        t++;
        h=h->next;
    }
    for (int i=0; i<2; i++) {
        a=f;
        for (int j=0; j<2-i; j++) {
            if (min[j]>min[j+1]) {
                double temp;
                temp=min[j];
                min[j]=min[j+1];
                min[j+1]=temp;
                struct worker te;
                te=*a;
                *a=*(a->next);
                *(a->next)=te;
            }
            a=a->next;
        }
    }
    output(n);
}

void best(struct worker *g){
    double startmin[3];
    double endmin[3];
    double total[3];
    int t;
    struct worker *h,*f,*a;
    a=f=h=g;
    t=0;
    while (h!=NULL) {
        startmin[t]=(h->start[0])*60+h->start[1]+(h->start[2])/60;
        t++;
        h=h->next;
    }
    h=g;
    t=0;
    while (h!=NULL) {
        endmin[t]=(h->end[0])*60+h->end[1]+(h->end[2])/60;
        t++;
        h=h->next;
    }
    for (int a=0; a<3; a++) {
        total[a]=endmin[a]-startmin[a];
    }
    for (int i=0; i<2; i++) {
        a=g;
        for (int j=0; j<2-i; j++) {
            if (total[j]<total[j+1]) {
                double temp;
                temp=total[j];
                total[j]=total[j+1];
                total[j+1]=temp;
                struct worker te;
                te=*a;
                *a=*(a->next);
                *(a->next)=te;
            }
            a=a->next;
        }
    }
    printf("%d",g->number);
}

void save(struct worker *h){
    FILE *fp1;
    char s[10];
    sprintf(s, "%d",h->date[0],h->date[1],h->date[2]);
    strcat(s,".txt");
    if ((fp1=fopen(s, "w"))!=0) {
        fwrite(h, sizeof(struct worker), 3, fp1);
    }
    fclose(fp1);
}

void reads(struct worker *i){
    FILE *fp2;
    char s[10];
    sprintf(s, "%d",i->date[0],i->date[1],i->date[2]);
    strcat(s,".txt");
    fp2=fopen(s, "r");
    fread(i, sizeof(struct worker), 3, fp2);
    fclose(fp2);
}